package com.gitee.huanminabc.jcommon.reflect;


import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: huanmin
 * @create: 2025-04-09 13:22
 **/
@Slf4j
public class ObjectUtil {
    //将对象转换为Map<String, Object>
    public static Map<String,Object> toMap(Object bean){
        Map<String, Object> map = new HashMap<>();
        try {
            Field[] publicFields = FieldUtil.getPublicFields(bean.getClass());
            for (Field field : publicFields) {
                Object value = field.get(bean);
                if (value != null) {
                    map.put(field.getName(), value);
                }
            }
        } catch (IllegalAccessException e) {
            log.error("Error converting object to map", e);
        }
        return map;
    }


    //将value转换为fieldType, 考虑溢出报错,如果类型兼容就直接转换
    public static Object convertValueType(Class<?> fieldType, Object value) {
         if (value == null) {
             return null;
         }
        if (fieldType == int.class || fieldType == Integer.class) {
            if (value instanceof Long) {
                long longValue = (Long) value;
                if (longValue > Integer.MAX_VALUE || longValue < Integer.MIN_VALUE) {
                    throw new IllegalArgumentException("Long value out of int range");
                }
                return (int) longValue;
            } else if (value instanceof BigInteger) {
                return ((BigInteger) value).intValueExact();
            }
            // 可以添加对其他类型的处理，例如String到int的转换
        } else if (fieldType == long.class || fieldType == Long.class) {
            if (value instanceof Integer) {
                return ((Integer) value).longValue();
            }else if (value instanceof BigInteger) {
                return ((BigInteger) value).longValueExact();
            }
        } else if (fieldType == double.class || fieldType == Double.class) {
            if (value instanceof Integer) {
                return ((Integer) value).doubleValue();
            } else if (value instanceof Long) {
                return ((Long) value).doubleValue();
            }
            // 可以添加对其他类型的处理，例如String到double的转换
        }else if (fieldType == float.class || fieldType == Float.class){
            if (value instanceof Integer) {
                return ((Integer) value).floatValue();
            } else if (value instanceof Long) {
                return ((Long) value).floatValue();
            } else if (value instanceof Double) {
                return ((Double) value).floatValue();
            }
        }else if (fieldType ==boolean.class || fieldType == Boolean.class){
            if (value instanceof String) {
                return Boolean.parseBoolean((String) value);
            }else if (value instanceof Number) {
                return ((Number) value).intValue() != 0;
            }
        }else if (fieldType.isEnum()) {
            return EnumUtil.castEnum(value, fieldType);
        }
        return value;
    }
}
